﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using Domen;

namespace Forma
{
    public class Komunikacija
    {
        TcpClient klijent;
        NetworkStream tok;
        BinaryFormatter formater;

        public bool PoveziSaServerom()
        {
            try
            {
                klijent = new TcpClient("127.0.0.1", 10000);
                tok = klijent.GetStream();
                formater = new BinaryFormatter();
                return true;
            }
            catch (Exception)
            {
                return false;
                
            }
        }

        public int DodajMesto(Mesto m)
        {
            TransferKlasa transfer = new TransferKlasa();
            transfer.TransferObjekat = m;
            transfer.Operacija = (int)Operacije.SACUVAJ_MESTO;
            formater.Serialize(tok, transfer);
            transfer = formater.Deserialize(tok) as TransferKlasa;
            return transfer.Signal;
        }

        public List<Mesto> VratiMesta()
        {
            TransferKlasa transfer = new TransferKlasa();
            transfer.Operacija = (int)Operacije.VRATI_MESTA;
            formater.Serialize(tok, transfer);
            transfer = formater.Deserialize(tok) as TransferKlasa;
            return transfer.TransferObjekat as List<Mesto>;
        }

        public void Zatvori()
        {
            TransferKlasa transfer = new TransferKlasa();
            transfer.Operacija = (int)Operacije.KRAJ;
            formater.Serialize(tok, transfer);
        }
    }
}
